package fi.helsinki.cs.ohtu10k.view;

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import org.joda.time.Duration;

/**
 * Class implementing a simple graphical duration selector using JComboBoxes. 
 * @author Thomas Nyman
 *
 */
@SuppressWarnings("serial")
public class DurationSelector extends JPanel {
	private JComboBox selectorHours;
	private JComboBox selectorMinutes;
	
	/**
	 * Creates a new duration selector.
	 */
	public DurationSelector() {
		selectorHours = new JComboBox(Constants.DURATIONS);
		selectorMinutes = new JComboBox(Constants.MINUTES);
		
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		add(selectorHours);
		add(Box.createRigidArea(new Dimension(10, 0)));
		add(selectorMinutes);
		add(Box.createHorizontalGlue());
	}
	
	/**
	 * Returns selected duration.
	 * @return selected duration.
	 */
	public Duration getDuration() {
		return new Duration(selectorHours.getSelectedIndex() * Constants.ONE_HOUR_IN_MILLISECONDS + 
							selectorMinutes.getSelectedIndex() * 15 * Constants.ONE_MINUTE_IN_MILLISECONDS);
	}
	
	/**
	 * Enables the combo box so that items can be selected. 
	 * When the combo box is disabled, items cannot be selected and 
	 * values cannot be typed into its field (if it is editable).
	 * @param b a boolean value, where true enables the component and false disables it
	 */
	@Override
	public void setEnabled(boolean b) {
		selectorHours.setEnabled(b);
		selectorMinutes.setEnabled(b);
	}

}
